package cl.usm.acceso.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class OrdenServicioDAO {

	private int nroOrden;
	private int numero;
	private int idSucursal;
	private String rut;
	private String tipoCliente;
	private int numConvenio;
	private int idRecarga;
	private Date fecha;
	private int prioridad;
	private int estado;
	
	public int getNroOrden() {
		return nroOrden;
	}

	public void setNroOrden(int nroOrden) {
		this.nroOrden = nroOrden;
	}
	
	public int getNumero() {
		return numero;
	}
	
	public void setNumero(int numero) {
		this.numero = numero;
	}
	
	public int getIdSucursal() {
		return idSucursal;
	}
	
	public void setIdSucursal(int idSucursal) {
		this.idSucursal = idSucursal;
	}
	
	public String getRut() {
		return rut;
	}
	
	public void setRut(String rut) {
		this.rut = rut;
	}
	
	public String getTipoCliente() {
		return tipoCliente;
	}
	
	public void setTipoCliente(String tipoCliente) {
		this.tipoCliente = tipoCliente;
	}
	
	public int getNumConvenio() {
		return numConvenio;
	}
	
	public void setNumConvenio(int numConvenio) {
		this.numConvenio = numConvenio;
	}
	
	public int getIdRecarga() {
		return idRecarga;
	}
	
	public void setIdRecarga(int idRecarga) {
		this.idRecarga = idRecarga;
	}
	
	public Date getFecha() {
		return fecha;
	}
	
	public void setFecha(Date fecha) {
		this.fecha = fecha;
	}
	
	public int getPrioridad() {
		return prioridad;
	}
	
	public void setPrioridad(int prioridad) {
		this.prioridad = prioridad;
	}
	
	public int getEstado() {
		return estado;
	}
	
	public void setEstado(int estado) {
		this.estado = estado;
	}

	public int crearNroOrden() {
		int result = -1;
		int newNroOrden = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Insert into orden_servicio (nroOrden, idSucursal, fecha) values (null, 1, now())";

			result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);	
			ResultSet rsKeyColumn = statement.getGeneratedKeys();
			if ( rsKeyColumn.next() ) { 
				newNroOrden = rsKeyColumn.getInt(1);  
			}
			
			statement.close();
			rsKeyColumn.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("NroOrden : " + newNroOrden);
		result = newNroOrden;
		
		return result;
	}
	
	public int seleccionar(int nroOrden) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
			String sql = "Select numero, idSucursal, rut, tipoCliente, numComvenio, idRecarga," +
					"fecha, prioridad, estado from orden_servicio where nroOrden = '" + nroOrden + "'";
	
			ResultSet resultSet = statement.executeQuery(sql);
			
			while (resultSet.next()) {
				setNumero(resultSet.getInt("idServicio"));
				setIdSucursal(resultSet.getInt("idSucursal"));
				setRut(resultSet.getString("rut"));
				setTipoCliente(resultSet.getString("tipoCliente"));
				setNumConvenio(resultSet.getInt("numConvenio"));
				setIdRecarga(resultSet.getInt("idRecarga"));
				setFecha(resultSet.getDate("fecha"));
				setPrioridad(resultSet.getInt("prioridad"));
				setEstado(resultSet.getInt("estado"));
				result = 1;
			}
	
			resultSet.close();
			statement.close();
			connection.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public int modificar(int nroOrden, int numero, int idSucursal, String rut, String tipoCliente,
			int numConvenio, int id_recarga, Date fecha, int prioridad, int estado) {
		int result = -1;
		try {
			InitialContext context = new InitialContext();
			DataSource dataSource = (DataSource) context.lookup("jdbc/Lavasoft");
			
			Connection connection = dataSource.getConnection();
			Statement statement = connection.createStatement();
		    
			String sql = "update registra set numero = '" + numero
					+ "', idSucursal = '" + idSucursal
					+ "', rut = '" + rut
					+ "', tipoCliente = '" + tipoCliente
					+ "', numConvenio = '" + numConvenio
					+ "', id_recarga = '" + id_recarga
					+ "', fecha = '" + fecha
					+ "', prioridad = '" + prioridad
					+ "', estado = '" + estado
					+ "' where nroOrden = '" + nroOrden + "'";
			result = statement.executeUpdate(sql);
	
			statement.close();
			connection.close();
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}	
}
